package com.apobates.jforum2.troll.replies.mapper;

import com.apobates.jforum2.troll.replies.entity.Posts;
import com.apobates.jforum2.troll.replies.stub.vo.ReplyVo;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import com.apobates.jforum2.troll.utils.lang.TriFunction;
import java.util.Optional;
import java.util.function.Function;

/**
 *
 * @author xiaofanku
 */
public class ReplyVoMapperEntity implements OptionalEntityTransformMapper<Posts, ReplyVo>{
    @Override
    public Optional<ReplyVo> transform(Optional<Posts> entity) {
        return entity.map(ins->{
            return mapper().apply(ins);
        });
    }

    public static Function<Posts, ReplyVo> mapper(){
        return posts -> {
            ReplyVo rv = new ReplyVo();
            rv.setId(posts.getId());
            rv.setContent(posts.getContent());
            //回复日期
            rv.setEntryDateTime(posts.getEntryDateTime());
            //作者:会员的ID
            rv.setMember(posts.getMemberId());
            rv.setNickname(posts.getMemberNickname());
            rv.setIpAddr(posts.getIpAddr());
            //话题的ID
            rv.setThreads(posts.getTopicId());
            //卷的ID
            rv.setVolumes(posts.getVolumesId());
            //版块的ID
            rv.setBoard(posts.getBoardId());
            //楼层|ranking
            rv.setFloor(posts.getFloorNumber());
            //ETC
            //是否是回复,true(1)表示回复,false(0)表示是楼主贴子的内容
            rv.setReply(posts.isReply());
            //状态,false(0)禁用,true(1)可用
            rv.setStatus(posts.isStatus());
            //编辑日期
            rv.setModifyDateTime(posts.getModifyDateTime());
            rv.setModifyMember(posts.getModifyMemberId());
            rv.setModifyNickname(posts.getModifyMemberNickname());
            String topicLink = produceTopicLink.apply(posts.getTopicId(), posts.getBoardId(), posts.getVolumesId());
            rv.setThreadsLink(topicLink);
            rv.setLink(generateLink(posts));
            return rv;
        };
    }
    
    public static TriFunction<Long,Long,Integer,String> produceTopicLink = (Long topicId, Long boardId, Integer boardgroupId)->{
        return String.format("/topic/%d-%d-%d.xhtml", topicId, boardId, boardgroupId);
    };
    
    public static String generateLink(Posts posts){
        if(null == posts){
            return "#";
        }
        return String.format("%s#posts-%d", produceTopicLink.apply(posts.getTopicId(), posts.getBoardId(), posts.getVolumesId()), posts.getId());
    }

}